<?php

// +----------------------------------------------------------------------
// | Static Plugin for ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2023 ThinkAdmin [ thinkadmin.top ]
// +----------------------------------------------------------------------
// | 官方网站: https://thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// | 免责声明 ( https://thinkadmin.top/disclaimer )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/think-plugs-static
// | github 代码仓库：https://github.com/zoujingli/think-plugs-static
// +----------------------------------------------------------------------

namespace app\index\controller;

use EasyWeChat\Factory;
use EasyWeChat\OfficialAccount\Application;
use think\admin\Controller;

class Index extends Controller
{
    /**
     * 微信调度器
     * @var Application
     */
    private $wechat;

    /**
     * 控制器初始化
     * @return $this
     */
    protected function initialize()
    {
        $config = config('easywechat.OfficialAccount');
        $this->wechat = Factory::officialAccount($config);
        $this->target = input('source') ?: $this->request->server('http_referer', $this->request->url(true));
        return $this;
    }
    public function index()
    {
        $data = [
            'url' => '',
            'openid' => ''
        ];
        session('openid', '222222');
        if ($code = input('code')) {
            $user = $this->wechat->oauth->userFromCode($code);
            var_dump(
                $user->getRaw()
            );
            if ($user->getId()) {
                $data['openid'] = $user->getId();
                session('openid', $user->getId());
            } else{
                session('openid', '7777777');
            }
        } else {
            $data['url'] = $this->wechat->oauth->redirect();
        }
        $this->fetch('index', $data);
    }
    public function alitransfer() {
//        Transfer::instance()->create()
    }
}
